using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Box2D.XNA;


namespace Elementum
{

    public class PlayerCollisionDetector : ContactDetector
    {
        public List<Body> buttonList = new List<Body>();
        public List<Body> uplList = new List<Body>();
        public List<Body> leftlList = new List<Body>();
        public List<Body> rightList = new List<Body>();
        protected List<Body> contactBodylList = new List<Body>();
        public float _normal;
        protected const float PI = 3.14159f;

        public PlayerCollisionDetector(Body body)
            : base(body)
        {
        }

        protected void checkList(List<Body> list)
        {
            foreach (Body b in list)
            {
                if (!contactBodylList.Contains(b))
                    foreach (Body body in list)
                        list.Remove(body);
            }
        }

        public override void BeginContact(Contact contact, Body contactBody, float normal)
        {
            //float normal = (float)Math.Atan2(-GetContactAngle(contact).Y, GetContactAngle(contact).X)*180/PI;
            ////Body contactBody = GetContactedBody(contact);
            //Object userData = contactBody.GetUserData();
            //normal += 360;
            //normal = normal % 360;
            _normal = normal;




            if (contactBody.GetUserData() == "ground" || 
                contactBody.GetUserData() == "door" ||
                  contactBody.GetUserData() == "snowball" || 
                contactBody.GetUserData() == "monster")
            {
                if (contactBody.GetUserData() == "door") {
                    GameDoor door =(GameDoor) contactBody.Owner;
                    if (door.IsOpened == true)
                        return;
                }


                if (normal > 45 && normal < 135)//button
                    if (!buttonList.Contains(contactBody))
                        buttonList.Add(contactBody);

                if (normal > 315 || normal < 45 && contactBody.GetUserData() != "monster")//right
                    if (!rightList.Contains(contactBody))
                        rightList.Add(contactBody);

                if (normal > 225 && normal < 315&&contactBody.GetUserData() != "monster")//up
                    if (!uplList.Contains(contactBody))
                        uplList.Add(contactBody);

                if (normal > 135 && normal < 225&&contactBody.GetUserData() != "monster")//left
                    if (!leftlList.Contains(contactBody))
                        leftlList.Add(contactBody);

            }
        }

        //public void pollingContact()
        //{

        //    contactBodylList.Clear();

        //    Contact contact = _body.GetWorld().GetContactList();
        //    while (contact != null)
        //    {
        //        Body b = GetContactedBody(contact);
        //        if (b != null)
        //        {
        //            if (!contactBodylList.Contains(b))
        //                contactBodylList.Add(b);

        //        }
        //        contact = contact.GetNext();
        //    }

        //    checkList(buttonList);
        //    checkList(leftlList);
        //    checkList(rightList);
        //    checkList(uplList);
        //}

        public override void EndContact(Contact contact, Body contactBody, float normal)
        {

            //Body contactBody = GetContactedBody(contact);
            //Gdx.app.log("this.body=", "B");
            _normal = normal;
            //if (contactBody == null)
            //    return;

            contactBodylList.Remove(contactBody);

            while (buttonList.Contains(contactBody))
                buttonList.Remove(contactBody);

            for (int i = 0; i < rightList.Count(); i++)
                rightList.Remove(contactBody);

            for (int i = 0; i < uplList.Count(); i++)
                uplList.Remove(contactBody);

            for (int i = 0; i < leftlList.Count(); i++)
                leftlList.Remove(contactBody);
        }
    }
}
